home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLR / glrduck / SoXtGLRRenderArea.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.1 KB  |  133 lines

  1. /******************************************************************************
  2.  * $Id: SoXtGLRRenderArea.C,v 1.1 1996/03/05 00:22:03 dave Exp $
  3.  * 
  4.  * Source File Name: SoXtGLRRenderArea.c++
  5.  * 
  6.  *             Copyright (c) 1991-96 Silicon Graphics, Inc.
  7.  *            SILICON GRAPHICS INC., Mountain View, California
  8.  *  
  9.  * Permission to use, copy, modify, distribute, and sell this software and
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that the name of Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Silicon Graphics.
  14.  * 
  15.  * The software is provided AS-IS and without WARRANTY of any kind,
  16.  * express, implied or otherwise, including without limitation, any
  17.  * warranty of merchantabilityl or fitness for a particular purpose.
  18.  * 
  19.  * In no event shall Silicon Graphics be liable for any special, incidental,
  20.  * indirect or consequential damages of any kind, or any damages whatsoever
  21.  * resulting from loss of use, data or profits, whether or not advised of the
  22.  * possibility of damage, and on any theory of liability, arising out of or in
  23.  * connection with the use or performance of this software. Contractor is 
  24.  * SILICON GRAPHICS, INC., 2011 N. Shoreline blvd., Mountain View, CA. 94039
  25.  * 
  26.  * Language: C++
  27.  * 
  28.  * Description: 
  29.  * 
  30.  * Revision History: 
  31.  *  02/08/96  ROE  Initial Coding.
  32.  *     
  33.  *****************************************************************************/
  34. #include "SoXtGLRRenderArea.h"
  35. #include <iostream.h>
  36.  
  37. GLint 
  38. SoXtGLRRenderArea::fallbackAttributes[] = {GLR_RGBA, GLR_RED_SIZE, 1, 
  39.                        GLR_GREEN_SIZE, 1, GLR_BLUE_SIZE, 
  40.                        1, GLR_DEPTH_SIZE, 16, 0};
  41.  
  42.  
  43. SoXtGLRRenderArea::SoXtGLRRenderArea(int attributes[],
  44.                      Widget parent,
  45.                      const char *name, 
  46.                      SbBool buildInsideParent, 
  47.                      SbBool getMouseInput,
  48.                      SbBool getKeyboardInput) :
  49.   SoXtRenderArea(parent, name, buildInsideParent, 
  50.          getMouseInput, getKeyboardInput), canvas(NULL), session(NULL)
  51. {
  52.   session = glrOpenSession(NULL);
  53.   if (session == NULL) {
  54.     cerr << "Couldn't get GLR session, check $GLR_SERVER. Using local rendering." << endl;
  55.     return;
  56.   }
  57.   GLrCanvasType canvasType;
  58.  
  59.   canvasType = glrGetCanvasType(session, attributes);
  60.   if (canvasType == NULL) {
  61.     canvasType = glrGetCanvasType(session, fallbackAttributes);
  62.     if (canvasType == NULL) {
  63.       cerr << "Couldn't find usable canvas type." << endl;
  64.       exit(1);
  65.     }
  66.   }
  67.   canvas = glrCreateCanvas(canvasType, NULL);
  68.   if (canvas == NULL) {
  69.     cerr << "Couldn't get a canvas." << endl;
  70.     exit(1);
  71.   }
  72.   glrEstablishRenderState(canvas);
  73. }
  74.  
  75. void SoXtGLRRenderArea::render(SoXtGLRRenderArea::Quality quality)
  76. {
  77.   if (quality == SoXtGLRRenderArea::HIGH && session) {
  78.     renderRemote(); // Rendering to GLR server.
  79.   }
  80.   else {
  81.     SoXtRenderArea::render(); // Rendering locally.
  82.   }
  83. }
  84.  
  85. void SoXtGLRRenderArea::renderRemote()
  86. {
  87.   GLint    status;
  88.   GLint    duration;
  89.   SbVec2s  size;
  90.   GLubyte *image = NULL;
  91.  
  92.  
  93.   // We need the size of the window.
  94.   size = getSize();
  95.   image = (GLubyte *) new GLubyte[size[0] * size[1] * 3]; // 4 of RGB mode.
  96.   if (image == NULL) {
  97.  
  98.   }
  99.  
  100.   status = glrBeginRenderInterval(canvas, size[0], size[1], 500, 30000);
  101.   cerr << "glrBeginRenderInterval = " << ((status) ? "yes" : "FAILED") 
  102.        << endl;
  103.  
  104.   glEnable(GL_DEPTH_TEST);
  105.  
  106.   // We want to invalidate the cache.
  107.   getGLRenderAction()->invalidateState();
  108.  
  109.   actualRedraw(); 
  110.  
  111.   glPixelStorei(GL_PACK_ALIGNMENT, 1);
  112.   glReadPixels(0, 0, size[0], size[1], GL_RGB, GL_UNSIGNED_BYTE, image);
  113.  
  114.   status = glrEndRenderInterval(canvas, &duration);
  115.   cerr << "glrEndRenderInterval = " << ((status) ? "yes" : "FAILED") << endl;
  116.   cerr << "duration = " << duration << endl;
  117.  
  118.   // Change context back to the local window.
  119.   glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
  120.  
  121.   glDrawBuffer(GL_BACK);
  122.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  123.   glDrawPixels(size[0], size[1], GL_RGB, GL_UNSIGNED_BYTE, image);
  124.  
  125.   glFinish();
  126.  
  127.   glXSwapBuffers(getDisplay(), getNormalWindow());
  128.  
  129.   if (image)
  130.     delete [] image;
  131. }
  132.  
  133.